*** 
/* *** Gomez Cram & Olbert 2023 ***
 * Measuring the Expected Effects of the Global Tax Reform *
forthcoming in the Review of Financial Studies

THIS CODE: Discontinuity Analysis for Event Day 8 October 2021 for Pillar 1 Rules Release 
- Prepares main dataset "Events_TAQ_18Feb2022" for RDD analysis: I
	*This dataset contains the stock returns for stock i on event date_day (in basis points)
	*This dataset also contains firm-level sorting variables and fundamentals from fundamentals_compustat_15Feb2022
- Runs RDD tests as reported in paper: II  
 

*/
* Last edited 2023-04-17


 
*** General Settings (globals for macros, version, etc.) ***

clear
*Directory Macros
glo dir = c(pwd)
glo raw = "$dir/../Data/Events_TAQ_data"
glo final = "$dir/../Data/Events_TAQ_data"
glo graphs = "$dir/../Figures"
glo tables = "$dir/../Tables"

*Multi Score RDD package
*net sj 20-4
*net install st0620
*net get st0620
//10% (i.e. profit before tax/revenue)  https://www.oecd.org/tax/beps/brochure-two-pillar-solution-to-address-the-tax-challenges-arising-from-the-digitalisation-of-the-economy-october-2021.pdf


*Figure options 
net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
set scheme cleanplots, perm

graph set window fontface "Arial"

set scheme s1color, perm



***** I. Prepare Data *****
use  "$raw/Events_TAQ_18Feb2022.dta", clear

* 1. Prepare variables
egen date_key=group(Date_day)
tab Date_day date_key


*Date variable 
gen date_1jul=(date_key==4)
gen date_8oct=(date_key==5)

*Running variable: Sales threshold
sum sort_size_sales, d
qui gen rv_10bn=sort_size_sales-12000   //12bn USD approx. 10bn EUR
qui gen rv_20bn=sort_size_sales-24000 	//24bn USD approx. 20bn EUR
 
*Running variable: Profitability threshold
merge m:1 gvkey year  using "fundamentals_compustat_15-20_v1", keepusing(pi pifo) keep(master match)
gen pillar1_profit= (pi / sort_size_sales) * 100
winsor2 pillar1_profit , cuts(1 99) replace

g rv_profit = pillar1_profit - 10

*Multiple cutoff treatment indicators  
gen treated_p1 = 0
replace treated_p1 = 1 if rv_profit>0 & rv_10bn>0 & rv_profit!=.
gen treated_p2 = 0
replace treated_p2 = 1 if rv_profit>0 & rv_20bn>0 & rv_profit!=.

*Sample selection 
g sort_intl_dummy_0 =(sort_intl_incomeshare_avg3>0 & sort_intl_incomeshare_avg3!=.)
tab sort_intl_dummy_0

keep if year ==2020

compress
save  "$final/Events_TAQ_18Feb2022_rdd.dta", replace



//II.2 Regressions 
use "$final/Events_TAQ_18Feb2022_rdd.dta", clear

*Baseline 10 and 20 bn threshold
local rv = "rv_10bn"  
eststo rdd1:  rdrobust return_stock  `rv' if date_8oct ==1  & (sort_intl_dummy_0==1 )  , c(0) p(1) all bwselect( msetwo )    
local rv = "rv_20bn"  
eststo rdd2:  rdrobust return_stock  `rv' if date_8oct ==1      & `rv'>-12000   , c(0) p(1) all bwselect( msetwo )   

*Multiple cutoffs including profitabilty threshold 
cap drop c?
g c1= 0 if _n==1
g c2 = 0 if _n==1
 
eststo rdd3: rdms return_stock  treated_p1 rv_profit rv_10bn if date_8oct ==1  & sort_intl_dummy_0==1   , cvar(c1 c2)   
estadd scalar   hl = e(H)[1,1] 
estadd scalar ssl = e(sampsis)[1,1] 
estadd scalar ssr = e(sampsis)[2,1] 
 
eststo rdd4: rdms return_stock  treated_p2 rv_profit rv_20bn if date_8oct ==1     , cvar(c1 c2)    // xnorm(xnorm)
estadd scalar   hl = e(H)[1,1] 
estadd scalar ssl = e(sampsis)[1,1] 
estadd scalar ssr = e(sampsis)[2,1] 
 
 
*Robustness regressions 
local rv = "rv_10bn"  
*Including covariates
eststo rdd1_r1:  rdrobust return_stock  `rv' if date_8oct ==1  & (sort_intl_dummy_0==1 )  , c(0) p(1) all bwselect( msetwo )    covs(pillar1_profit sort_etr_gaap_avg3)

*Donut RDD 
local rv = "rv_10bn"  
eststo rdd1_r2:  rdrobust return_stock  `rv' if date_8oct ==1  & (sort_intl_dummy_0==1 ) & !inrange(rv_10bn,-1200,0) , c(0) p(1) all bwselect( msetwo )  

*Placebo date  
eststo rdd1_r3:  rdrobust return_stock  `rv' if date_8oct ==0  & (sort_intl_dummy_0==1 )  , c(0) p(1) all bwselect( msetwo )    

*Placebo threshold
cap drop rv_plac
qui gen rv_plac =  sort_size_sales-5000
eststo rdd1_r4:  rdrobust return_stock  rv_plac if date_8oct ==1  & sort_intl_dummy_0==1 , c(0) p(1) all bwselect( msetwo )   
    
 


*Output  
estout rdd? rdd1_r? , /// 
c(b(fmt(%9.2f) nostar) t(par([ ]) fmt(%9.2f)) p(par fmt(%9.2f)))   mlabels(,   ) numbers  ///
stats(N h_l h_r N_h_l N_h_r hl ssl ssr, fmt(%9.0fc %9.0fc %9.0fc %9.0fc  %9.0fc   %9.3f) labels("Orig. Obs." "Bandwidth l" "Bandwidth r" "Observations left" "Observations right" )) ///  
replace label
  

  
//II.3 RD Plots: Figure 6
*Local linar
local bw = "5000"
local rv = "rv_10bn"
rdplot return_stock `rv' if date_8oct ==1   & inrange(`rv',-`bw',`bw')   , p(1) /// nbins(100 100)    /// //{stSans}
graph_options(  xtitle("Sales around Pillar 1 Size Threshold (in USD million)", margin(medium)) ytitle(Stock Return in bp)   ///
 xlabel(-`bw'(1000)`bw', format(%13.0fc) labsize(small))  yscale(range(-30 15)) ylabel( -30(5)15, format(%13.0fc) labsize(small)) ///
 legend(off))  //adjust manually for colors and boxes
qui gr export "$graphs\rd_p1_intl_Oct8_comb.png",  replace  as(png)	

 
*2nd Order Polynomial
local bw = "5000"
local rv = "rv_10bn"
rdplot return_stock `rv' if date_8oct ==1   & inrange(`rv',-`bw',`bw')   , p(2) /// nbins(100 100)    /// //{stSans}
graph_options(  xtitle("Sales around Pillar 1 Size Threshold (in USD million)", margin(medium)) ytitle(Stock Return in bp)   ///
 xlabel(-`bw'(1000)`bw', format(%13.0fc) labsize(small))  yscale(range(-30 15)) ylabel( -30(5)15, format(%13.0fc) labsize(small)) ///
 legend(off))  //adjust manually for colors and boxes
qui gr export "$graphs\rd_p2_intl_Oct8_comb.png",  replace  as(png)	
 

 



//II.3 Appendix Material: Density Plot Online Appendix C.7
use "$final/Events_TAQ_18Feb2022_rdd.dta", clear
local rv = "rv_10bn"  //rv_10bn
local b = "5000"
display `b'
keep if  inrange(`rv' ,-`b',`b') 

rddensity `rv'  , c(0) plot nohist level(95) p(3)   plot_range(-`b' `b')    /// // plot_range(-10000 10000)
graph_opt(ytitle(Local-Polynomial Density, margin(small)) xtitle("Sales around Pillar 1 Size Threshold (in USD million)", margin(small)) ///
graphregion(color(white))  xlabel(-5000(1000)5000 , format(%13.0fc) labsize(small))  ylabel( ,   labsize(small)) ///
legend(label(1 "95% CI left") label(2 "95% CI right") label(3 "Local Density Estimate left") label(4 "Local Density Estimate right") ))
	 
qui gr export "$graphs\rd_rvdensity_`rv'.pdf",  replace  as(pdf)	

 
 